package com.hqyj.lls.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类。将时间转换为JSON格式
 */
public class DateUtils {

    public static String getDateJSON(Object object) {
        return getDateJSON(object,"yyyy.MM.dd G 'at' HH:mm:ss z");
    }

    public static String getDateJSON(Object object,String dateFormat) {
        //1.获取objectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        //2.不用时间戳
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //3.定义smipleDateFormat
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        //4.设置输出格式
        objectMapper.setDateFormat(simpleDateFormat);
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
